﻿using System;
using System.Collections.Generic;
using System.Text;
using MapCenterSystem.DataInfo;
using System.Drawing;
using System.Net;
using System.IO;
using System.Drawing.Imaging;

namespace MapCenterSystem.ServiceProvider.GoogleMap
{
    public class GoogleMapProviderEx
    {
        private string lang = "vi";// language 

        protected string map_url = "http://mt{0}.google.com/vt/hl={1}&x={2}&y={3}&z={4}";
        protected string satellite_url = "http://khm{0}.google.com/kh/v={1}&x={2}&y={3}&z={4}";
        protected string hybrid_url = "http://mt{0}.google.com/vt/lyrs=y&hl={1}&x={2}&y={3}&z={4}";
        protected string satellite_ver = "54";

        private Image[,] image = new Image[3, 3];

        private int GetServerResponse(Point tile, int max)
        {
            return (tile.X + 2 * tile.Y) % max;
        }

        private byte[] GetGoogleMap(MapInfoEx map)
        {
            if (map.CheckExist())
                return map.ConvertToByte();

            #region old
            //string strRequest = string.Empty;
            //switch (map.MapType)
            //{
            //    case 0: //map
            //        strRequest = string.Format(map_url, GetServerResponse(map.Tile, 4), lang, map.Tile.X, map.Tile.Y, map.Zoom);
            //        break;
            //    case 1: //satellite
            //        strRequest = string.Format(satellite_url, GetServerResponse(map.Tile, 4), satellite_ver, map.Tile.X, map.Tile.Y, map.Zoom);
            //        break;
            //    case 2: //hybrid
            //        strRequest = string.Format(hybrid_url, GetServerResponse(map.Tile, 4), lang, map.Tile.X, map.Tile.Y, map.Zoom);
            //        break;
            //}

            //WebRequest webrequest = WebRequest.Create(strRequest);
            //((HttpWebRequest)webrequest).UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5";
            //WebResponse webresponse = webrequest.GetResponse();
            //Stream stream = webresponse.GetResponseStream();
            //StreamReader streamReader = new StreamReader(stream);
            //MemoryStream memStream = new MemoryStream();
            //byte[] respBuffer = new byte[1024];
            //int byteRead = stream.Read(respBuffer, 0, respBuffer.Length);
            //while (byteRead > 0)
            //{
            //    memStream.Write(respBuffer, 0, byteRead);
            //    byteRead = stream.Read(respBuffer, 0, respBuffer.Length);
            //}
            //return memStream.GetBuffer();
            #endregion

            return null;
        }

        public Image GetMapImage(MapInfoEx map)
        {
            map.MapStore = Path.Combine(map.MapStore, "Google");
            InitListImage(map);
            Image memImage = CombineImage();
            Image currentImage = new Bitmap(map.Size.Width, map.Size.Height);
            Graphics g = Graphics.FromImage(currentImage);
            int k = map.MapCenter.X % 256;
            int t = map.MapCenter.Y % 256;
            int temp1 = 256 + k - currentImage.Width / 2;
            int temp2 = 256 + t - currentImage.Height / 2;
            g.DrawImage(memImage, new Rectangle(0, 0, currentImage.Width, currentImage.Height),
                new Rectangle(temp1, temp2, currentImage.Width, currentImage.Height), GraphicsUnit.Pixel);
            g.Dispose();

            return currentImage;
        }

        public void InitListImage(MapInfoEx map)
        {
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    Point currentTile = new Point(map.Tile.X + i - 1, map.Tile.Y + j - 1);
                    MapInfoEx mapTemp = new MapInfoEx();
                    //mapTemp.EarthPoint = map.EarthPoint;
                    mapTemp.MapStore = map.MapStore;
                    mapTemp.Size = map.Size;
                    mapTemp.MapSource = map.MapSource;
                    mapTemp.Zoom = map.Zoom;
                    mapTemp.Tile = currentTile;
                    mapTemp.MapType = map.MapType;
                    Image temp = map.GetMapFromByte(GetGoogleMap(mapTemp));
                    if (!mapTemp.CheckExist())
                    {
                        Bitmap bitmap = new Bitmap(temp);
                        bitmap.Save(mapTemp.GetFullPath(), ImageFormat.Png);
                        bitmap.Dispose();
                    }
                    temp.Dispose();
                    image[i, j] = new Bitmap(mapTemp.GetFullPath());
                }
            }
        }

        private Image CombineImage()
        {
            Image memImage = new Bitmap(3 * 256, 3 * 256);
            Graphics g = Graphics.FromImage(memImage);
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    g.DrawImage(image[i, j], i * 256, j * 256);
                }
            }
            g.Dispose();
            return memImage;
        }
    }
}
